package com.shzu.jwc.ty.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.shzu.jwc.ty.bean.UserStudentInfo;
import com.shzu.jwc.ty.bean.UserStudentInfoExample;
import com.shzu.jwc.ty.common.MD5Util;
import com.shzu.jwc.ty.common.ReadExcel;
import com.shzu.jwc.ty.dao.UserStudentInfoMapper;

@Service
public class StudentInfoservice {

	@Autowired
	UserStudentInfoMapper mapper;
	
	public List<String> getcollege()
	{
		return mapper.getcollege();
	}
	
	public List<String> getmajor()
	{
		return mapper.getmajor();
	}
	
	public List<Integer> getyear()
	{
		return mapper.getyear();
	}
	
	public List<UserStudentInfo> getstudent()
	{
		return mapper.selectByExample(null);
	}
	
	public Integer isexits(String userStudentNum) {
		UserStudentInfoExample example=new UserStudentInfoExample();
		UserStudentInfoExample.Criteria criteria=example.createCriteria();
		criteria.andUserStudentInfoNumEqualTo(userStudentNum);
		return (int) mapper.countByExample(example);
	}
	
	public List<UserStudentInfo> selectstudent(Integer userStudentInfoYear,String userStudentInfoCollege){
		UserStudentInfoExample example=new UserStudentInfoExample();
		UserStudentInfoExample.Criteria criteria=example.createCriteria();
		if(userStudentInfoYear!=0)
			criteria.andUserStudentInfoYearEqualTo(userStudentInfoYear);
		if(!userStudentInfoCollege.equals("0"))
			criteria.andUserStudentInfoCollegeEqualTo(userStudentInfoCollege);
		return mapper.selectByExample(example);
	}
	
	public List<UserStudentInfo> searchstudent(String userStudentInfoNum){
		UserStudentInfoExample example=new UserStudentInfoExample();
		UserStudentInfoExample.Criteria criteria=example.createCriteria();
		criteria.andUserStudentInfoNumEqualTo(userStudentInfoNum);
		return mapper.selectByExample(example);
	}
	
	public Integer delete(Integer id) {
		return mapper.deleteByPrimaryKey(id);
	}
	
	public Integer insert(UserStudentInfo userStudentInfo) {
		return mapper.insert(userStudentInfo);
	}
	
	public void batchImport(String name,MultipartFile file) {
		ReadExcel readExcel=new ReadExcel();
		List<UserStudentInfo> userStudentInfos=readExcel.getExcelInfo(name, file);
		if(userStudentInfos.size()!=0) {
			for(Integer i=0;i<userStudentInfos.size();i++)
			{
				//System.out.println(userStudentInfos.get(i));
				mapper.insertstudent(userStudentInfos.get(i));
			}
		}
	}
	
	public Integer repass(Integer userStudentInfoId) {
		UserStudentInfo userStudentInfo=new UserStudentInfo();
		userStudentInfo.setUserStudentInfoId(userStudentInfoId);
		userStudentInfo.setUserStudentInfoPassword(MD5Util.encrypt("shzu"));
		return mapper.updateByPrimaryKeySelective(userStudentInfo);
	}
}
